﻿@{
    ViewBag.Title = "调查";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model ElingOA6_DSJCJ.Model.Paper
@{
    ElingOA6_DSJCJ.BLL.PaperAnswerOption paperAnswerOptionBLL = new ElingOA6_DSJCJ.BLL.PaperAnswerOption();
    decimal paperId = Model.FPaperId;

    List<ElingOA6_DSJCJ.Model.PaperTopic> paperTopicList = ViewData["paperTopicList"] == null ?
        new List<ElingOA6_DSJCJ.Model.PaperTopic>() : ViewData["paperTopicList"] as List<ElingOA6_DSJCJ.Model.PaperTopic>;
    int topicCount = paperTopicList.Count;
}
@if (false)
{
    <script src="../../Scripts/jquery-vsdoc.js" type="text/javascript"></script>
   
    <script src="../../Scripts/json2.js" type="text/javascript"></script>  
}

<script src="@Url.Content("~/Scripts/json2.js", true)" type="text/javascript"></script>

<script>
    var paperId=@paperId;

</script>

<script>

    function getData(flag) {
        var paperHtml = '{"FPaperTopicId":0,"FAnswer":"","FIsCorrect":0,"FWriteAnswer":"","FPaperAnswerManagerID":0}';
        var listHtml = '[]';
        var str = "";
        var modelList = JSON.parse(listHtml);
        $('.pt2').each(function (index) {
            var jisu = index + 1;
            var paperModel = JSON.parse(paperHtml);
            paperModel.FPaperTopicId = $(this).find('td:eq(0)').find('input:hidden').val();
            if ($(this).find(':radio').length) {
                paperModel.FAnswer = $(this).find(':radio:checked').val();
                var kjk = $(this).find('td:eq(0)').find('textarea').text(); //获取自填内容
                if (kjk != "") {
                    paperModel.FWriteAnswer = kjk;
                }
                if (paperModel.FAnswer == undefined) {//判断
                    alert("第" + jisu + "题未选择!");
                    flag = false;
                    return flag;
                }

                if ($("#rdo" + jisu).attr("checked") && kjk == "") {
                    alert("第" + jisu + "题其它答案未填空!");
                    flag = false;
                    return flag;
                }
                else if ($("#rdo" + jisu).attr("checked") == false && kjk != "") {
                    $("#rdo" + jisu).attr("checked", true);
                }
            }
            else if ($(this).find(':checkbox').length) {
                var jklj = "";
                $(this).find('td:eq(0)').find('input:checked').each(function () {
                    jklj += $(this).val();
                });

                paperModel.FAnswer = jklj;
                var kjk = $(this).find('td:eq(1)').find('textarea').text(); //获取自填内容
                if (kjk != "") {
                    paperModel.FWriteAnswer = kjk;
                }
                if (paperModel.FAnswer == "") {//判断
                    alert("第" + jisu + "题未选择");
                    flag = false;
                    return flag;
                }
                if ($("#che" + jisu).attr("checked") == "checked" && kjk == "") {
                    alert("第" + jisu + "题其它答案未填空!");
                    flag = false;
                    return flag;
                }
                else if (!$("#che" + jisu).is("checked") && kjk != "") {
                    $("#che" + jisu).attr("checked", true);
                }
            }
            else {

                paperModel.FWriteAnswer = $(this).find('td:eq(0)').find('textarea').text();
                if (paperModel.FWriteAnswer == "") {//判断
                    alert("第" + jisu + "道简答题未填写");
                    flag = false;
                    return flag;
                }

            }
            modelList.push(paperModel);
        });
        if (modelList.length > 0) {
            str = JSON.stringify(modelList);
        }
        $('#hidpaper').val(str);
        return flag;
    }
    /*保存*/
    function save() {
        var flag = true;
        //checkForm(flag);
        flag = getData(flag);
        if (flag == true) {
            form1.submit(); //提交
        }
    }

    function checkForm(flag) {
        var count = $("#hidFPaperTopicId").val();
        var str = "第";

        for (var i = 1; i <= count; i++) {
            var tempElem = document.getElementsByName("che" + i);
            if (tempElem.length > 0) {
                for (var j = 0; j < tempElem.length; j++) {
                    if (tempElem[j].checked) {
                        var tempTxa = document.getElementsByName("txa" + i);
                        if (tempTxa[0].innerText == "") {
                            str += i + ",";
                            flag = false;
                        }
                    }
                }
            }
        }
        if (str.length > 2) {
            str = str.substring(0, str.length - 1);
        }
        str += "题其它答案未填写!\r";
        if (flag == false) {
            alert(str);
        }
        return flag;

    }


    //当单选框失去焦点时
    function cheQiTa2(obj) {
        $tr = $(obj).parent().parent();
        if ($tr.find('td:eq(2)').find('textarea') != "undefined") {
            var tid = $tr.find('td:eq(2)').find('textarea').attr("id");
            $tr.find('td:eq(2)').find('textarea').text("");
        }
    }
    //判断文本框是否为空
    function estiMate(obj) {
        var val = obj.value;
        if (val != "") {
            $tr = $(obj).parent().parent();
            if ($tr.find('td:eq(0)').find(':checkbox') != "undefined") {
                $tr.find('td:eq(0)').find(':checkbox').attr('checked', true);
            }
            if ($tr.find('td:eq(0)').find(':radio') != "undefined") {
                $tr.find('td:eq(0)').find(':radio').attr('checked', true);
                $tr.find('td:eq(0)').find(':radio').focus();
            }
        }
    }
    //当复选框选中状态为假时
    function cheQiTa(obj) {
        if ($(obj).attr("checked") != "checked") {
            $tr = $(obj).parent().parent();
            if ($tr.find('td:eq(2)').find('textarea') != "undefined") {
                var tid = $tr.find('td:eq(2)').find('textarea').attr("id");
                $tr.find('td:eq(2)').find('textarea').text("");
            }
        }
    }
</script>

<style>
    .top-bg
    {
        background: #f5f5f5;
        border-bottom: #aaaaaa 2px solid;
    }
    .titname
    {
        color: #1555a9;
        font-size: 20px;
        font-weight: 700;
        padding-bottom: 4px;
    }
    .titintro
    {
        color: #3c3c3d;
        padding-bottom: 10px;
    }
    .content
    {
        margin: 0px auto;
        width: 900px;
        position: relative;
        
        
    }
    .next
    {
       
        text-align: center;
        position: relative;
        height: 40px;
    }
    .nextbnt
    {
        background: url(@Url.Content("~/Content/images/bntbg.gif")) no-repeat;
        width: 122px;
        height: 36px;
        line-height: 36px;
        display: inline-block;
        text-align: center;
        font-size: 14px;
        font-family: "幼圆";
        color: #fff;
        cursor: pointer;
        border: 0;
        margin: 0 10px;
    }
    .nextbnt a
    {
        color: #fff;
        text-decoration: none;
    }
    
</style>
@using (Html.BeginForm("Save", "Paper", FormMethod.Post, new { id = "form1", @class = "edit_form" }))
{ 
    <table width="1000" align="center" cellspacing="0" style="margin-top: 0px;">
        <tr>
            <td align="center" bgcolor="#FFFFFF">
                <table width="980" align="center" cellspacing="0" style="margin-top: 0px;">
                    <tr>
                        <td width="36">
                            <img src="@Url.Content("~/Content/images/icon2.jpg", true)" width="36" height="31" />
                        </td>
                        <td width="100%" align="left" class="title" style="background: url(@Url.Content("~/Content/images/titlebg.jpg", true)) repeat-x">
                            问卷调查 <a href="/Paper/PaperAnswer?paperId=@paperId">查看结果</a></td>
                    </tr>
                    <tr>
                        <td width="100%" align="left" bgcolor="#FFFFFF" colspan="2">
                            <table width="100%" border="0" align="left" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td colspan="2">
                                        @if (Model.FIsRecordName == "N")
                                        {
                                            <table align="center" style="width: 800px; border-width: 1px; margin: 2px; border-spacing: 10px">
                                                <tr>
                                                    <td>
                                                        调查对象名称</td>
                                                    <td>@Html.TextBox("SurveyTargetName", "")</td>
                                                    <td>
                                                        调查对象联系方式</td>
                                                    <td>@Html.TextBox("SurveyTargetContact", "")</td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        单位名称</td>
                                                    <td>@Html.TextBox("SurverUnitName", "")</td>
                                                    <td>
                                                        通讯地址</td>
                                                    <td>@Html.TextBox("SurverAddress", "")</td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        邮编</td>
                                                    <td>@Html.TextBox("SurverPostCode", "")</td>
                                                    <td>
                                                        电子邮件</td>
                                                    <td>@Html.TextBox("SurverEMail", "")</td>
                                                </tr>
                                            </table>
                                        }
                                        <div class="top-bg fixed">
                                        </div>
                                        <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" style="margin-top: 10px;
                                            margin-bottom: 10px;">
                                            <tr>
                                                <td align="center" class="db">
                                                    <div class="titname" id="titname">
                                                        @Model.FTitle</div>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <div class="content" id="titintro" style="margin-top:5px; margin-buttom:5px">
                                                        @Model.FContent
                                                    </div>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <div class="content">
                                                        <table align="left">
                                                            @{int topicSeqId = 1;
                                                              List<ElingOA6_DSJCJ.Model.PaperAnswerOption> paperAnswerOptionList = new List<ElingOA6_DSJCJ.Model.PaperAnswerOption>();
                                                            }
                                                            @foreach (ElingOA6_DSJCJ.Model.PaperTopic paperTopicModel in paperTopicList)
                                                            {
                                                                
                                                                <tr class="pt2">
                                                                    <td>
                                                                        <table align="left">
                                                                            <tr clas>
                                                                                <td align="left" colspan="2" style="font-weight: bold">
                                                                                    @topicSeqId、@paperTopicModel.FTopic
                                                                                    <input type="hidden" value="@paperTopicModel.FPaperTopicId" />
                                                                                </td>
                                                                            </tr>
                                                                            @if (paperTopicModel.FTopicType == "A")
                                                                            {
                                                                                <tr>
                                                                                    <td colspan="2">
                                                                                        <textarea style="overflow:auto;" rows="@paperTopicModel.FShortAnswerCount" cols="110"></textarea>
                                                                                    </td>
                                                                                </tr>
                                                                                topicSeqId++;
                                                                            }
                                                                            else
                                                                            {
                                                                                paperAnswerOptionList = paperAnswerOptionBLL.GetModelList(paperTopicModel.FPaperTopicId);


                                                                                switch (paperTopicModel.FTopicType)
                                                                                {
                                                                                    case "R":
                                                                                        {
                                                                                            //单选框

                                                                                            foreach (var p in paperAnswerOptionList)
                                                                                            {

                                                                                                if (p.FIsRestsAnswer == "Y")
                                                                                                {
                                                                                <tr>
                                                                                    <td valign='top'>
                                                                                        &nbsp;<input type="radio" id="rdo@topicSeqId" name="@paperTopicModel.FPaperTopicId" value="@p.FAnswerNo" onblur='cheQiTa2(this)' />&nbsp;@p.FAnswerNo
                                                                                        .其它</td>
                                                                                    <td>
                                                                                        &nbsp;&nbsp;<textarea onblur='estiMate(this);' id="txa@topicSeqId" name="txa@topicSeqId" style="overflow: auto;" rows="@p.FRestsAnswerCount " cols="50"></textarea>
                                                                                    </td>
                                                                                </tr>
                                                                                                }
                                                                                                else
                                                                                                {
                                                                                <tr>
                                                                                    <td colspan="2">
                                                                                        &nbsp;<input type="radio" name="@paperTopicModel.FPaperTopicId" value="@p.FAnswerNo" />&nbsp;@p.FAnswerNo
                                                                                        .@p.FAnswerContent</td>
                                                                                </tr>
                                                                                                }

                                                                                            }

                                                                                            break;
                                                                                        }
                                                                                    case "C":
                                                                                        {
                                                                                            //复选框
                                                                                            foreach (var p in paperAnswerOptionList)
                                                                                            {

                                                                                                if (p.FIsRestsAnswer == "Y")
                                                                                                {
                                                                                <tr>
                                                                                    <td valign='top'>
                                                                                        &nbsp;<input type="checkbox" id="che@topicSeqId" name="@paperTopicModel.FPaperTopicId" value="@p.FAnswerNo" onblur='cheQiTa2(this)' />&nbsp;@p.FAnswerNo
                                                                                        .其它</td>
                                                                                    <td>
                                                                                        &nbsp;&nbsp;<textarea onblur='estiMate(this);' id="txa@topicSeqId" name="txa@topicSeqId" style="overflow: auto;" rows="@p.FRestsAnswerCount " cols="50"></textarea>
                                                                                    </td>
                                                                                </tr>
                                                                                                }
                                                                                                else
                                                                                                {
                                                                                <tr>
                                                                                    <td colspan="2">
                                                                                        &nbsp;<input type="checkbox" name="@paperTopicModel.FPaperTopicId" value="@p.FAnswerNo" />&nbsp;@p.FAnswerNo
                                                                                        .@p.FAnswerContent</td>
                                                                                </tr>
                                                                                                }

                                                                                            }
                                                                                            break;
                                                                                        }
                                                                                }
                                                                                topicSeqId++;
                                                                            }
                                                                            <tr>
                                                                                <td colspan="2">
                                                                                    &nbsp </td>
                                                                            </tr>
                                                                        </table>
                                                                    </td>
                                                                </tr>
                                                                
                                                            }
                                                            @* @Html.Raw(ViewData["paperTopicList"].ToString())*@
                                                        </table>
                                                    </div>
                                                </td>
                                            </tr>
                                        </table>
                                        <div class="next">
                                            <span class="nextbnt" onclick="save()"><a href="javascript:void(0)" title="提交">提交</a></span>
                                        </div>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    @Html.Hidden("hidpaper", "")
    @Html.Hidden("hidFPaperId", paperId)
    @Html.Hidden("hidFPaperTopicId", topicCount)
}
